package com.fanshuai.tree.trie.main;

import com.fanshuai.tree.trie.ITrie;
import com.fanshuai.tree.trie.MultiWordTrie;
import com.fanshuai.tree.trie.Trie;
import com.fanshuai.tree.trie.WordToken;
import com.fanshuai.tree.trie.pinyin.PinyinTrie;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

public class PerformanceMain {
    static String textPath = "/Users/fanshuai/keyword/样本.csv";

    static String patternWordsPath = "/Users/fanshuai/keyword/正则词.csv";
    static String pinyinWordsPath = "/Users/fanshuai/keyword/拼音词.csv";
    static String normalWordsPath = "/Users/fanshuai/keyword/普通词.csv";

    static Trie trie = new Trie();
    static PinyinTrie pinyinTrie = new PinyinTrie();
    static MultiWordTrie multiWordTrie = new MultiWordTrie();

    static {
        try {
            List<String> normalWords = Files.readAllLines(Paths.get(normalWordsPath));
            trie.addWords(normalWords);

            List<String> pinyinWords = Files.readAllLines(Paths.get(pinyinWordsPath));
            pinyinWords = pinyinWords.stream().map(word -> {
                return word.replace("(拼)", "").replace("（拼）", "");
            }).collect(Collectors.toList());
            pinyinTrie.addWords(pinyinWords);

            List<String> multiWords = Files.readAllLines(Paths.get(patternWordsPath));
            multiWordTrie.addWords(multiWords);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void printTrie() {
        System.out.println(trie.size());
        System.out.println(pinyinTrie.size());
        System.out.println(multiWordTrie.size());

        System.out.println(trie.getAllWords());
        System.out.println(pinyinTrie.getAllWords());
        System.out.println(multiWordTrie.getAllWords());
    }

    private static void TestTrie(ITrie trie) throws Exception {
        List<String> lines = Files.readAllLines(Paths.get(textPath));

        int i = 0;
        for (String line : lines) {
            i++;
            long start = System.currentTimeMillis();
            List<WordToken> tokens = trie.match(line);
            long spend = System.currentTimeMillis() - start;

            System.out.println("hits=" + tokens);
            System.out.println("i=" + i + ", spend=" + spend + "ms");
        }
    }

    public static void main(String[] args) throws Exception {
        printTrie();

        //TestTrie(trie);
        //TestTrie(pinyinTrie);
        TestTrie(multiWordTrie);
    }
}

